<?php
class CF_App
{
	protected static $_instance = null;
	
	protected $_path = null;
	
	protected $_controller = null;
	protected $_view = null;
	protected $_defaultViewClass = 'CF_App_View_Template';
	
	protected function __construct(){}
	protected function __clone(){}
	
	/**
	 * @return CF_App
	 */
	public static function getInstance()
	{
		if(null === self::$_instance){
			self::$_instance = new self();
		}
		
		return self::$_instance;
	}
	
	public function getPath()
	{
		return $this->_path;
	}
	
	public function setPath($path)
	{
		$this->_path = $path;
		$module = $this->getControler()->getModule();
		
		# register autoload module path
		CF_Loader::addIncludePath($path . "modules/$module");
		
		# register autoload models path
		CF_Loader::addIncludePath($path . 'models');
		
		# add controller path
		$this->getControler()->addPath($path . "modules/$module/controllers/");
		# add view path
		$this->getView()->setPath($path . "modules/$module/views/");
		
		return $this;	
	}
	
	/**
	 * @return CF_App_Controller
	 */
	public function & getControler()
	{
		if(null === $this->_controller){
			$this->_controller = new CF_App_Controller();	
		}
		return $this->_controller;
	}
	
	/**
	 * @return CF_App_View_Interface
	 */
	public function & getView()
	{
		if(null === $this->_view){
			$this->_view = new $this->_defaultViewClass;
		}
		return $this->_view;
	}
	
	public function run()
	{
		$this->getControler()->dispatch();
		return $this;
	}
}